<?php

namespace App\Admin\Forms\User;

use App\Http\Controllers\Api\Repositories\WxUserRepositores;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UserMemberForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return $this->response()
                ->error('无权限.');
        }
        $id = $input['id'];
        $days = $input['days'];
        if($days <= 0){
            return $this->response()
                ->warning('增加的天数应大于0')
                ->refresh();
        }
        WxUserRepositores::vip($id, $days);

        return $this->response()
            ->success('操作成功.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);
        $this->datetime('end_time', '到期时间')->disable()->help('不可直接修改')->default(UserUtils::get_user_member_time($this->payload["id"]));
        $this->number('days', '增加天数')->help('增加该用户会员的天数');
    }

}
